
#include <BPainter>
#include <BRender>
#include <BViewer>
#include <BPhysics>
#include <BCone>
#include <BCube>
#include <BReadWrite>
#include <BImage>

#include <BCube>
#include <BSphere>
#include <BCylinder>
#include <BCapsule>

#include "DynamicScene.h"
#include "BasicsHandler.h"

BasicsHandler::BasicsHandler()
{
	_group = new BGroup();

	BPhysics* physics = new BPhysics();
	physics->setMass(1);

	BCube* cube = new BCube(0.2, 10);
	cube->setName("cube");
	cube->setColor(150, 120, 100);

	btTransform transform;
	transform.setIdentity();
	for (int k = 0; k < 3; k++)
	{
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				BString name("cube");
				name << '-' << k << '-' << i << '-' << j;
				BNode* node = new BNode(name);
				node->setPhysics(physics);
				node->addShape(cube);
				node->setPosition(0.5 * i, 0.5 * j, 4 + 0.5 * k);
				_group->insert(node);
			}
		}
	}

	if (BSphere* sphere = new BSphere(0.1))
	{
		sphere->setName("sphere");
		sphere->setColor(120, 150, 100);
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				BString name("sphere");
				name << '-' << i << '-' << j;
				BNode* node = new BNode(name);
				node->setPosition(0.2 * i, 0.2 * j, 6);
				node->setPhysics(physics);
				node->addShape(sphere);
				_group->insert(node);
			}
		}
	}

	if (BCylinder* cylinder = new BCylinder(0.1, 0.6, 24, 6, 6))
	{
		cylinder->setColor(220, 150, 100);
		for (int i = 0; i < 10; i++)
		{
			for (int j = 0; j < 10; j++)
			{
				BString name("cylinder");
				name << '-' << i << '-' << j;
				BNode* node = new BNode(name);
				node->setPosition(0.2 * i, 0.2 * j, 8);
				node->setPhysics(physics);
				node->addShape(cylinder);
				_group->insert(node);
			}
		}
	}

	BPhysics* heavyPhysics = new BPhysics();
	heavyPhysics->setMass(100);
	BNode* node = new BNode("capsules");
	node->setPosition(0, 0, 10);
	node->setPhysics(heavyPhysics);
	for (int i = 0; i < 10; i++)
	{
		for (int j = 0; j < 10; j++)
		{
			BString name("sphere");
			name << '-' << i << '-' << j;
			BCapsule* capsule = new BCapsule(0.1, 0.3, 24, 6, 6);
			capsule->setName(name);
			capsule->setColor(220, 100, 100);
			capsule->setPosition(0.2 * i, 0.2 * j, 0);
			node->addShape(capsule);
		}
	}
	_group->insert(node);
}
BasicsHandler::~BasicsHandler()
{

}

void BasicsHandler::attached(BViewer* viewer)
{
	viewer->scene()->insert(_group);
}
void BasicsHandler::detached(BViewer* viewer)
{
	viewer->scene()->remove(_group);
}

void BasicsHandler::paint(BPainter& painter)
{
	BViewer* viewer = this->viewer();
	painter.setLineWidth(10);
	painter.setColor(BColor(0, 1, 0));
	painter.drawRect(10, 10, viewer->width() - 20, viewer->height() - 20);
}
void BasicsHandler::render(BRender& render)
{
	render.setLighting(false);
	render.setDepthTest(false);
	render.setLineSmooth(true);
	render.setLineWidth(1);
	
}
